package com.gzy.literature.controller;

import com.github.pagehelper.PageInfo;
import com.gzy.literature.entity.UserFavorites;
import com.gzy.literature.service.UserFavoritesService;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;

@RestController
@RequestMapping("userFavorites")
public class UserFavoritesController extends HttpServlet {

    @Resource
    private UserFavoritesService userFavoritesService;

    /**
     * 分页查询
     *
     * @param userFavorites 筛选条件
     * @param pageNum 页码，从1开始
     * @param pageSize 页面大小
     * @return 查询结果
     */
    @GetMapping("/page")
    public ResponseEntity<PageInfo<UserFavorites>> queryByPage(
            @RequestBody(required = false) UserFavorites userFavorites,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        if (pageNum <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        if (pageSize <= 0) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        PageInfo<UserFavorites> pageInfo = this.userFavoritesService.queryDll(userFavorites, pageNum, pageSize);
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<UserFavorites> queryById(@PathVariable("id") String id) {
        // 手动验证ID（可选）
        if (id == null || id.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        return ResponseEntity.ok(this.userFavoritesService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param userFavorites 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<UserFavorites> add(@RequestBody UserFavorites userFavorites) {
        // 手动验证userFavorites（示例）
        if (userFavorites == null || userFavorites.getFavoriteTime() == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        return ResponseEntity.ok(this.userFavoritesService.insert(userFavorites));
    }

    /**
     * 编辑数据
     *
     * @param userFavorites 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<UserFavorites> edit(@RequestBody UserFavorites userFavorites) {
        if (userFavorites == null || userFavorites.getUserId() == null || userFavorites.getWorkId() == null || userFavorites.getFavoriteTime() == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }

        return ResponseEntity.ok(this.userFavoritesService.update(userFavorites));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") String id) {
        // 手动验证ID（可选）
        if (id == null || id.isEmpty()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(false);
        }

        return ResponseEntity.ok(this.userFavoritesService.deleteById(id));
    }
}